package com.zhang;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.domain.User;
import com.zhang.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class Select {


    @Autowired
    private UserMapper userMapper;

    //1.根据Id查询(单个User)
    @Test
    public void test1() {
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    //2.根据Id集合批量查询(多个User)
    @Test
    public void test2() {


        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);


    }

    //3.根据Map<String,Object>查询(多个User)
    @Test
    public void test3() {


        //1.查询条件
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", "张飞");
        map.put("username", "赵云");//它只会查询赵云,不会查询张飞

        //2.进行查询
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);


    }

    //4.根据条件构造器查询(单个User)
    @Test
    public void test4() {

        //1.查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", "张飞");

        //2.进行查询
        User user = userMapper.selectOne(wrapper);
        System.out.println(user);

    }

    //5.根据条件构造器查询(多个User)
    @Test
    public void test5() {
        //1.查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", "张飞").or().eq("username", "赵云");

        //2.进行查询
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);

    }

    //6.根据条件构造器查询(总记录数)
    @Test
    public void test6() {
        //1.查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", "张飞").or().eq("username", "赵云");

        //2.进行查询
        Integer count = userMapper.selectCount(wrapper);
        System.out.println(count);

    }

    //7.根据条件构造器查询(List<Map<String.object>>)
    @Test
    public void test7() {
        //1.查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", "张飞").or().eq("username", "赵云");

        //2.进行查询
        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

    //8.根据指定条件查询符合条件的用户 （只返回第一个字段值） 有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    @Test
    public void test8() {

        //1.查询条件 (查询名字姓张的 且 密码>111的)
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("username","password").likeRight("username","张").gt("password","111");

        //2.查询
        List<Object> list = userMapper.selectObjs(queryWrapper);
        list.forEach(System.out::println);
    }

    //9.根据Ipage+条件构造器查询(Ipage)
    @Test
    public void test9() {
        //1.查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("password", "123");

        //2.分页查询
        Page<User> page = new Page<>(2, 2);
        userMapper.selectPage(page, wrapper);

        //3.打印数据
        //当前页数据
        List<User> userList = page.getRecords();
        userList.forEach(System.out::println);
        //总记录数:
        System.out.println("总记录数:" + page.getTotal());
        //每页大小:
        System.out.println("每页大小:" + page.getSize());
        //当前页码:
        System.out.println("当前页码:" + page.getCurrent());

    }

    //10.根据Ipage+条件构造器查询(IPage<Map<String, Object>>)
    @Test
    public void test10() {

    }


}
